package com.hmdp.config;

import com.hmdp.interceptor.LoginInterceptor;
import com.hmdp.interceptor.RefreshTokenInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;


@Configuration
public class MvcConfig implements WebMvcConfigurer {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 这里注册拦截器细节，拦截器的拦截顺序：
        // 所有的拦截器注册后默认的order都是0，order越小执行优先级越高
        // 如果没有指定拦截器的order值，默认是按照注册的顺序来执行
        // 为了严谨和安全，这里指定order的值

        //登录拦截器
        registry.addInterceptor(new LoginInterceptor())
                //.addPathPatterns("/**")//拦截所有路径
                .excludePathPatterns(
                        "/user/login",
                        "/user/code",
                        "/blog/hot",
                        "/upload/**",
                        "/shop/**",
                        "/shop-type/**",
                        "/voucher/**"
                ).order(1);//放行的路径

        //刷新拦截器
        registry.addInterceptor(new RefreshTokenInterceptor(stringRedisTemplate))
                .addPathPatterns("/**").order(0);
    }
}
